package com.jsgt.greekfire.ui.mainassets.adapter

import android.content.Intent
import android.os.Bundle
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.fhx.core.base.BaseActivity
import com.fhx.core.utils.PriceFormatUtil
import com.jsgt.greekfire.R
import com.jsgt.greekfire.bean.MainAssetsBean
import com.jsgt.greekfire.common.util.NetDataManager
import com.jsgt.greekfire.event.AssetsEvent
import kotlinx.android.synthetic.main.activity_currency_list.*
import kotlinx.android.synthetic.main.activity_currency_list.recycler
import kotlinx.android.synthetic.main.fragment_main_assets.*
import kotlinx.android.synthetic.main.view_title.*
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class CurrencyListActivity : BaseActivity() {
    private var toSymbol: String? = null
    private var fromSymbol: String? = null
    private var assetlist: ArrayList<MainAssetsBean>? = null
    private val mainAssetsAdapter by lazy {
        MainAssetsAdapter(this, R.layout.item_currency_list)
    }

    override fun getLayoutId(): Int = R.layout.activity_currency_list
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        toSymbol = intent.getStringExtra("toSymbol")
        fromSymbol = intent.getStringExtra("fromSymbol")
    }

    override fun main() {
        title_tv?.text = resources.getString(R.string.currency_selection_title)
        recycler.setLayoutManager(LinearLayoutManager(this, RecyclerView.VERTICAL, false))
        recycler.adapter = mainAssetsAdapter
        mainAssetsAdapter.setOnItemClickListener { adapter, view, position ->
            val intent = Intent()
            intent.putExtra("currentSymbol", mainAssetsAdapter.data.get(position).name)
            intent.putExtra("assetGuid", mainAssetsAdapter.data.get(position).assetGuid)
            intent.putExtra("assetBalance", mainAssetsAdapter.data.get(position).amount)
            setResult(RESULT_OK, intent)
            finish()
        }
    }

    override fun onResume() {
        super.onResume()
        NetDataManager.getUserAssets()
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun onAssetsEvent(e: AssetsEvent?) {
        if (assetlist != null) {
            assetlist?.clear()
        } else {
            assetlist = java.util.ArrayList()
        }
        for (index in NetDataManager.assetsList.indices) {
            var mainAssetsBean = MainAssetsBean()
            mainAssetsBean.assetId = NetDataManager.assetsList.get(index).display
            mainAssetsBean.assetGuid = NetDataManager.assetsList.get(index).assetGuid
            if (NetDataManager.assetsList.get(index).assetName.equals("USDT")) {
                mainAssetsBean.image = R.mipmap.main_asset_usdt_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("MET")) {
                mainAssetsBean.image = R.mipmap.main_asset_met_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("ZWT")) {
                mainAssetsBean.image = R.mipmap.main_asset_zwb_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("LTT")) {
                mainAssetsBean.image = R.mipmap.main_asset_ltb_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("JJT")) {
                mainAssetsBean.image = R.mipmap.main_asset_jjb_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("FHT")) {
                mainAssetsBean.image = R.mipmap.main_asset_fhb_logo
            } else if (NetDataManager.assetsList.get(index).assetName.equals("JFT")) {
                mainAssetsBean.image = R.mipmap.main_asset_jft_logo
            }
            mainAssetsBean.name = NetDataManager.assetsList.get(index).assetName
            mainAssetsBean.amount = NetDataManager.assetsList.get(index).assetBalance.toString()
            var num = PriceFormatUtil.format(
                NetDataManager.assetsList.get(index).assetBalance?.times(NetDataManager.assetsList.get(index).price!!),
                4
            )
            mainAssetsBean.rmbrice = "≈$ " + num
            assetlist!!.add(mainAssetsBean)
        }
        mainAssetsAdapter.replaceData(assetlist!!)

    }

}